//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    array: [{
      message: 'foo',
    }, {
      message: 'bar'
    }],
    array: [{
      message: 'foo',
    }, {
      message: 'bar'
    }],
    length: 4,
    objectArray: [{
        id: 5,
        unique: 'unique_5'
      },
      {
        id: 4,
        unique: 'unique_4'
      },
      {
        id: 3,
        unique: 'unique_3'
      },
      {
        id: 2,
        unique: 'unique_2'
      },
      {
        id: 1,
        unique: 'unique_1'
      },
      {
        id: 0,
        unique: 'unique_0'
      },
    ],
    item: {
      index: 0,
      msg: 'this is a template',
      time: '2016-09-15'
    },
  },
  onLoad: function() {

    wx.canIUse('console.log')
    wx.canIUse('CameraContext.onCameraFrame')
    wx.canIUse('CameraFrameListener.start')
    wx.canIUse('Image.src')
    //app使用全局函数 小程序初始化完成调用
    var a = wx.getLaunchOptionsSync()
    console.log(a)
    app.test(89757)

    wx.request({
      url: 'http://fa.51dreaming.com/api/demo/test', //fastadmin测试接口
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
      }
    })


    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  bindViewTap: function(e) {
    console.log(e)
    console.log(e.mark.myMark === "last") // true
    console.log(e.mark.anotherMark === "leaf") // true
  },
  switch: function(e) {
    const length = this.data.objectArray.length
    for (let i = 0; i < length; ++i) {
      const x = Math.floor(Math.random() * length)
      const y = Math.floor(Math.random() * length)
      const temp = this.data.objectArray[x]
      this.data.objectArray[x] = this.data.objectArray[y]
      this.data.objectArray[y] = temp
    }
    this.setData({
      objectArray: this.data.objectArray
    })
  },
  //事件处理函数
  // bindViewTap: function() {
  //   wx.navigateTo({
  //     url: '../logs/logs'
  //   })
  // },
  showmodal: function() {
    wx.showModal({
      title: '提示',
      content: '引入js成功',
    })
  },
  // bindViewTap: function(event) {
  //   console.log(event)
  //   console.log(event.currentTarget.dataset.alphaBeta === "1") // - 会转为驼峰写法
  //   console.log(event.currentTarget.dataset.alphabeta === "2") // 大写会转为小写
  // },
  tapName: function(e) {
    console.log(e)
    console.log(e.currentTarget.dataset.hi)
  },
  tap1: function(e) {
    console.log(e)
  },
  tap2: function(e) {
    console.log(e)
  },
  tap3: function(e) {
    console.log(e)
  },
  tap4: function(e) {
    console.log(e)
  },
  tap5: function(e) {
    console.log(e)
  },
  touchstart: function(e) {
    console.log(e)
  },
  touchmove: function(e) {
    console.log(e)
  },
  touchend: function(e) {
    console.log(e)
  },

  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })

  },
  onReachBottom: function() {
    console.log("上拉事件触发")
    // Do something when page reach bottom.
  },
  onPullDownRefresh: function() {
    console.log("下拉事件触发")
    // Do something when pull down.
  },
})